package com.youaishujuhui.devops.web.controller.channel;

import java.util.ArrayList;
import java.util.List;

import org.nutz.dao.Cnd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.youaishujuhui.devops.web.domain.channel.Channel;
import com.youaishujuhui.devops.web.exception.CustomException;
import com.youaishujuhui.devops.web.exception.ExceptionType;
import com.youaishujuhui.devops.web.po.Select2PO;
import com.youaishujuhui.devops.web.service.channel.ChannelService;
import com.youaishujuhui.devops.web.service.game.GameProcessService;
import com.youaishujuhui.devops.web.util.syslog.SystemLog;
import com.youaishujuhui.devops.web.vo.Result;

@Controller
@RequestMapping("/channel/channel")
public class ChannelController {
	
	@Autowired
    private ChannelService channelService;
	
	@Autowired
	private GameProcessService gameProcessService;
	
    @RequestMapping("/page")
    public ModelAndView page(){
        return new ModelAndView("channel/channel");
    }
    
    @SystemLog(module = "渠道管理", oper = "修改渠道")
    @RequestMapping("/edit")
    @ResponseBody
    public Result<String> edit(Channel po){
        Result<String> result = new Result<>();
        
        Channel oldChannel = channelService.fetch(po.getChannelId());
        
        int existProcessCnt = gameProcessService.count(Cnd.where("gameId", "=", po.getGameId()).
        		and("channelId", "=", po.getChannelId()).
        		and("state", "=", 1));
        
        if (existProcessCnt > 0 && (!oldChannel.getNameEn().equals(po.getNameEn()))) {
        	result.setCode(Result.FAIL);
            result.setMsg("此渠道已有游戏开服，不允许修改渠道英文名");
            return result;
        }
        
        if (existProcessCnt > 0 && (oldChannel.getGameId() != po.getGameId())) {
        	result.setCode(Result.FAIL);
            result.setMsg("此渠道已有游戏开服，不允许修改渠道所属游戏");
            return result;
        }
        
        if (existProcessCnt > 0 && (!oldChannel.getProperty().equals(po.getProperty()))) {
        	result.setCode(Result.FAIL);
            result.setMsg("此渠道已有游戏开服，不允许修改渠道出机方");
            return result;
        }
        
        channelService._update(po);
        result.setCode(Result.SUCCESS);
        result.setMsg("修改成功！");
        return result;
    }
    
    @SystemLog(module = "渠道管理", oper = "删除渠道")
    @RequestMapping("/del")
    @ResponseBody
    public Result<String> delete(Integer id){
        Result<String> result = new Result<>();
        
        if (gameProcessService.count(Cnd.where("channelId", "=", id).
        		and("state", "=", 1)) > 0) {
        	result.setCode(Result.FAIL);
            result.setMsg("还有属于此渠道的游戏服运行，不允许删除");
            return result;
        }
        
        channelService.delete(id);
        
        result.setCode(Result.SUCCESS);
        result.setMsg("删除成功！");
        return result;
    }
    
    @RequestMapping("/query")
    @ResponseBody
    public Result<List<Channel>> query(){
        
        Result<List<Channel>> result = new Result<>();

        List<Channel> list = channelService.queryAll();
        
        result.setData(list);
        result.setCode(Result.SUCCESS);

        return result;
    }
    
    @RequestMapping("/findById")
    @ResponseBody
    public Result<Channel> findById(Integer id){
        Result<Channel> result = new Result<>();
        Channel po = channelService.fetch(id);
        if(po == null){
            result.setCode(Result.FAIL);
            result.setMsg(String.format("记录已不存在！"));
        } else {
        	po = channelService._fetchLinks(po, null);
            result.setCode(Result.SUCCESS);
            result.setData(po);
        }

        return result;
    }
    
    @RequestMapping("/add")
    @ResponseBody
    @SystemLog(module = "渠道管理", oper = "添加渠道")
    public Result<String> add(Channel po) throws Exception {
        Result<String> result = new Result<>();
        
        try {
        	result.setCode(Result.SUCCESS);
            result.setMsg("添加成功");
            channelService._insert(po);
        } catch (Exception e) {
        	e.printStackTrace();
            throw new CustomException(ExceptionType.SYS_FAIL);
        }
        
        return result;
    }
    
    @RequestMapping("/queryByGameId")
    @ResponseBody
    public Result<List<Select2PO>> queryByGameId(int gameId){
        
        Result<List<Select2PO>> result = new Result<>();

        List<Channel> list = channelService.query(Cnd.where("gameId", "=", gameId));
        List<Select2PO> polist = new ArrayList<Select2PO>();
        if (list != null) {
        	for (Channel channel : list) {
        		Select2PO po = new Select2PO();
        		po.setId(String.valueOf(channel.getChannelId()));
        		po.setText(channel.getNameCn());
        		polist.add(po);
        	}
        }
        
        result.setData(polist);
        result.setCode(Result.SUCCESS);

        return result;
    }
}
